package com.gsc.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.gsc.domain.ResponseResult;
import com.gsc.domain.entity.Category;
import com.gsc.domain.entity.Link;
import com.gsc.domain.vo.CategoryVo;
import com.gsc.domain.vo.ExcelCategoryVo;
import com.gsc.domain.vo.PageVo;
import com.gsc.enums.AppHttpCodeEnum;
import com.gsc.service.CategoryService;
import com.gsc.service.LinkService;
import com.gsc.utils.BeanCopyUtils;
import com.gsc.utils.PathUtils;
import com.gsc.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author gscstart
 * @create 2023/2/20-10:29
 */
@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询分类接口
     * @return
     */
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        return categoryService.listAllCategory();
    }

    /**
     * TODO
     * Excel表格导出
     * @PreAuthorize("@ps.hasPermissions('content:category:export')") : 给接口加权限控制，看用户是否有这个权限
     * 需要对导出分类的接口做权限控制。
     * @param response
     */

    @PreAuthorize("@ps.hasPermissions('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        //设置下载文件的请求头
        String name = PathUtils.generateFileName();
        try {
            WebUtils.setDownLoadHeader(name+".xlsx",response);
            //获取需要导出的数据
//            List<CategoryVo> categoryVos = (List<CategoryVo>) categoryService.listAllCategory();
            List<Category> list = categoryService.list();
            //带有excel注解的集合
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(list, ExcelCategoryVo.class);

            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常
            // 如果出现异常，响应json数据
            ResponseResult result = ResponseResult.okResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }

    }


    @GetMapping("/list")
    public ResponseResult categoryPageList(Integer pageNum, Integer pageSize, Category category){
        PageVo pageVo = categoryService.categoryPageList(pageNum,pageSize,category);
        return ResponseResult.okResult(pageVo);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult selectCategoryById(@PathVariable("id") Long id){
        Category category = categoryService.getById(id);
        return ResponseResult.okResult(category);
    }

    @PutMapping
    public ResponseResult putCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteCategory(@PathVariable("id") Long id){
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }




}
